package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/12/14.
 */
@Controller
@RequestMapping(value = "page/winnersPrize")
public class WinnersPrizeController {

    @Autowired
    private WinnersPrizeService winnersPrizeService;

    @Autowired
    private TokenController tokenController;

    @Autowired
    private ManageIntegralService manageIntegralService;

    @Autowired
    private JPushController jPushController;

    @Autowired
    private StudentIntegralService studentIntegralService;

    @RequestMapping(value = "/add")
    public synchronized
    @ResponseBody
    Object winnersPrizeAdd(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            String name = studentApp.getStudentname();
            int number = ((int) (Math.random() * 5000)) + 1;
            IntegralManage integralManage = new IntegralManage();
            List<IntegralManage> integralManageList = manageIntegralService.selectByAll(integralManage);
            integralManage = integralManageList.get(integralManageList.size() - 1);
            StudentIntegral studentIntegral1 = studentIntegralService.selectByPrimaryKey(studentId);
            if (studentIntegral1.getLotterynumber()>0) {
                StudentIntegral studentIntegral = new StudentIntegral();
                studentIntegral.setStudentid(studentId);
                studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() - 1);
                studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                if (number == 1) {
                    if (integralManage.getFirstprizenum() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("num", 1);
                        objectMap.put("message", "您中奖了，中了一等奖");
                        IntegralManage integralManage1 = new IntegralManage();
                        integralManage1.setFirstprizenum(integralManage.getFirstprizenum() - 1);
                        integralManage1.setId(integralManage.getId());
                        manageIntegralService.updateByPrimaryKeySelective(integralManage1);
                        Date date1 = new Date();
                        long time = date1.getTime();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String Time = simpleDateFormat.format(time);
                        WinnersPrize winnersPrize = new WinnersPrize();
                        winnersPrize.setWinningtime(Time);
                        winnersPrize.setPrizetype(0);
                        winnersPrize.setWinnersid(studentId);
                        winnersPrize.setPrizenumber(1);
                        winnersPrize.setPrizeperiod(integralManage.getChangetopnum());
                        winnersPrizeService.insertSelective(winnersPrize);
                        jPushController.testSendPush("恭喜！！！学号为" + studentId + "的" + name + "同学中了一等奖");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "您未中奖，下次努力");
                    }
                } else if (number <= 4 && number > 1) {
                    if (integralManage.getSecondprizenum() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("num", 2);
                        objectMap.put("message", "您中奖了，中了二等奖");
                        IntegralManage integralManage1 = new IntegralManage();
                        integralManage1.setSecondprizenum(integralManage.getSecondprizenum() - 1);
                        integralManage1.setId(integralManage.getId());
                        manageIntegralService.updateByPrimaryKeySelective(integralManage1);
                        Date date1 = new Date();
                        long time = date1.getTime();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String Time = simpleDateFormat.format(time);
                        WinnersPrize winnersPrize = new WinnersPrize();
                        winnersPrize.setWinningtime(Time);
                        winnersPrize.setPrizetype(0);
                        winnersPrize.setWinnersid(studentId);
                        winnersPrize.setPrizenumber(2);
                        winnersPrize.setPrizeperiod(integralManage.getChangetopnum());
                        winnersPrizeService.insertSelective(winnersPrize);
                        jPushController.testSendPush("恭喜！！！学号为" + studentId + "的" + name + "同学中了二等奖");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "您未中奖，下次努力");
                    }
                } else if (number <= 14 && number > 4) {
                    if (integralManage.getThirdprizenum() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("num", 3);
                        objectMap.put("message", "您中奖了，中了三等奖");
                        IntegralManage integralManage1 = new IntegralManage();
                        integralManage1.setThirdprizenum(integralManage.getThirdprizenum() - 1);
                        integralManage1.setId(integralManage.getId());
                        manageIntegralService.updateByPrimaryKeySelective(integralManage1);
                        Date date1 = new Date();
                        long time = date1.getTime();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String Time = simpleDateFormat.format(time);
                        WinnersPrize winnersPrize = new WinnersPrize();
                        winnersPrize.setWinningtime(Time);
                        winnersPrize.setPrizetype(0);
                        winnersPrize.setWinnersid(studentId);
                        winnersPrize.setPrizenumber(3);
                        winnersPrize.setPrizeperiod(integralManage.getChangetopnum());
                        winnersPrizeService.insertSelective(winnersPrize);
                        jPushController.testSendPush("恭喜！！学号为" + studentId + "的" + name + "同学中了三等奖");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "您未中奖，下次努力");
                    }
                } else if (number <= 44 && number > 14) {
                    if (integralManage.getFourthprizenum() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("num", 4);
                        objectMap.put("message", "您中奖了，中了四等奖");
                        IntegralManage integralManage1 = new IntegralManage();
                        integralManage1.setFourthprizenum(integralManage.getFourthprizenum() - 1);
                        integralManage1.setId(integralManage.getId());
                        manageIntegralService.updateByPrimaryKeySelective(integralManage1);
                        Date date1 = new Date();
                        long time = date1.getTime();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String Time = simpleDateFormat.format(time);
                        WinnersPrize winnersPrize = new WinnersPrize();
                        winnersPrize.setWinningtime(Time);
                        winnersPrize.setPrizetype(0);
                        winnersPrize.setWinnersid(studentId);
                        winnersPrize.setPrizenumber(4);
                        winnersPrize.setPrizeperiod(integralManage.getChangetopnum());
                        winnersPrizeService.insertSelective(winnersPrize);
                        jPushController.testSendPush("恭喜！学号为" + studentId + "的" + name + "同学中了四等奖");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "您未中奖，下次努力");
                    }
                } else if (number <= 194 && number > 44) {
                    if (integralManage.getFifthprizenum() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("num", 5);
                        objectMap.put("message", "您中奖了，中了五等奖");
                        IntegralManage integralManage1 = new IntegralManage();
                        integralManage1.setFifthprizenum(integralManage.getFifthprizenum() - 1);
                        integralManage1.setId(integralManage.getId());
                        manageIntegralService.updateByPrimaryKeySelective(integralManage1);
                        Date date1 = new Date();
                        long time = date1.getTime();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String Time = simpleDateFormat.format(time);
                        WinnersPrize winnersPrize = new WinnersPrize();
                        winnersPrize.setWinningtime(Time);
                        winnersPrize.setPrizetype(0);
                        winnersPrize.setWinnersid(studentId);
                        winnersPrize.setPrizenumber(5);
                        winnersPrize.setPrizeperiod(integralManage.getChangetopnum());
                        winnersPrizeService.insertSelective(winnersPrize);
                        jPushController.testSendPush("恭喜！学号为" + studentId + "的" + name + "同学中了五等奖");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "您未中奖，下次努力");
                    }
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "您未中奖，下次努力");
                }
                objectMap.put("lotteryNumber",studentIntegral1.getLotterynumber() - 1);
            }else {
                objectMap.put("code", 203);
                objectMap.put("message", "没有抽奖次数");
            }
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "此学生没有权限");
        }
        return objectMap;
    }

    @RequestMapping(value = "/sel")
    public synchronized
    @ResponseBody
    Object winnersPrizeSel(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            IntegralManage integralManage = new IntegralManage();
            List<IntegralManage> integralManageList = manageIntegralService.selectByAll(integralManage);
            if (integralManageList!=null&&integralManageList.size()>0) {
                WinnersPrize winnersPrize = new WinnersPrize();
                winnersPrize.setWinnersid(studentId);
                List<WinnersPrize> winnersPrizeList = winnersPrizeService.selectBySid(winnersPrize);
                if (winnersPrizeList != null && winnersPrizeList.size() > 0) {
                    Map<String, Object> map;
                    List<Map> mapList = new ArrayList<Map>();
                    for (int i = 0; i < winnersPrizeList.size(); i++) {
                        map = new HashMap<String, Object>();
                        winnersPrize = winnersPrizeList.get(i);
                        map.put("id", winnersPrize.getId());
                        map.put("prizeNumber", winnersPrize.getPrizenumber());
                        map.put("prizeType", winnersPrize.getPrizetype());
                        map.put("winningTime", winnersPrize.getWinningtime());
                        mapList.add(map);
                    }
                    map = new HashMap<String, Object>();

                    map.put("changeAddress", integralManageList.get(0).getChangeaddress());
                    map.put("changeTime", integralManageList.get(0).getChangetime());
                    objectMap.put("change", map);
                    objectMap.put("data", mapList);
                    objectMap.put("code", 200);
                    objectMap.put("message", "成功");
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "此学生没有中奖记录");
                }
            } else {
                objectMap.put("code", 203);
                objectMap.put("message", "没有开启抽奖");
            }
        } else {
            objectMap.put("code", 201);
            objectMap.put("message", "此学生没有权限");
        }
        return objectMap;
    }
}
